package com.pinda.controller;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.pinda.commonutils.Result;
import com.pinda.commonutils.StatusCode;
import com.pinda.feign.PdOrderCargoFeignClient2;
import com.pinda.feign.PdTransportOrderFeignClient;
import com.pinda.service.IPdTransportOrderService;
import com.pinda.work.entity.PdTransportOrder;
import com.pinda.service.IPdTransportOrderService;
import com.pinda.service.IPdTransportOrderService;
import com.pinda.work.DTO.TransportOrderDTO;
import com.pinda.work.entity.PdTransportOrder;
import com.pinda.work.vo.TransportOrderQueryView;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;
import java.util.UUID;

/**
 * <p>
 * 运单表 前端控制器
 * </p>
 *
 * @author lc
 * @since 2023-06-14
 */
@RestController
@RequestMapping("/pd-transport-order")
public class PdTransportOrderController {
    @Autowired
    private IPdTransportOrderService transportOrderService;

    @Autowired
    private IPdTransportOrderService iPdTransportOrderService;

    @Autowired
    private PdTransportOrderFeignClient pdTransportOrderFeignClient;

    @Autowired
    private IPdTransportOrderService pdTransportOrderService;

    @Autowired
    private PdOrderCargoFeignClient2 pdOrderCargoFeignClient2;

    @GetMapping("/lcquerypagetransportorderview/{id}")
    public Result<List<TransportOrderQueryView>> querypagetransportorderviewController(@PathVariable("id") String id){
        return new Result<>(true, StatusCode.OK,"查询成功",this.pdTransportOrderFeignClient.querypagetransportorderviewController(id));
    }
    /**
     * 根据订单id获取运单信息
     *
     * @param orderId 订单id
     * @return 运单信息
     */
    @GetMapping("/orderId/{orderId}")
    public TransportOrderDTO findByOrderId(@PathVariable(name = "orderId") String orderId) {
        TransportOrderDTO dto = new TransportOrderDTO();
        PdTransportOrder transportOrder = transportOrderService.findByOrderId(orderId);
        if (transportOrder != null) {
            BeanUtils.copyProperties(transportOrder, dto);
        } else {
            dto = null;
        }
        return dto;
    }

    /**
     * 快递员交件完成往运单表添加新的运单
     * @param orderId
     * @return
     */
    @GetMapping("/savePdTransportOrder/{orderId}")
    public Boolean savePdTransportOrder(@PathVariable("orderId") String orderId){
        UUID uuid = UUID.randomUUID();
        long mostSignificantBits = uuid.getMostSignificantBits();
        long leastSignificantBits = uuid.getLeastSignificantBits();
        String pureNumericUUID = "AD"+Math.abs(mostSignificantBits) + "" + Math.abs(leastSignificantBits);
        PdTransportOrder pdTransportOrder = new PdTransportOrder();
        String substring = pureNumericUUID.substring(0, 20);
        pdTransportOrder.setId(substring);
        pdTransportOrder.setOrderId(orderId);
        pdOrderCargoFeignClient2.updOrderTypeTransportOrder(orderId,substring);
        return pdTransportOrderService.savePdTransportOrder(pdTransportOrder);
    }

    /**
     * 快递已妥投修改为已签收
     * @param OrderId
     * @return
     */
    @PostMapping("UpdTransportOrder/{id}")
    public boolean upd(@PathVariable("id") String OrderId){
        UpdateWrapper updateWrapper=new UpdateWrapper();
        updateWrapper.eq("order_id",OrderId);
        updateWrapper.setSql("status=5");
        return iPdTransportOrderService.update(updateWrapper);
    }

    /**
     * 根据多个订单id查询运单信息
     *
     * @param ids
     * @return
     */
    @GetMapping("orderIds")
    public List<TransportOrderDTO> findByOrderIds(@RequestParam(name = "ids") List<String> ids) {
        LambdaQueryWrapper<PdTransportOrder> wrapper = new LambdaQueryWrapper();
        wrapper.in(PdTransportOrder::getOrderId, ids);
        List<PdTransportOrder> transportOrders = transportOrderService.list(wrapper);
        return transportOrders.stream().map(item -> {
            TransportOrderDTO dto = new TransportOrderDTO();
            BeanUtils.copyProperties(item, dto);
            return dto;
        }).collect(Collectors.toList());
    }
}
